package com.onenine.app.view;


import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * 可在边界内拖动的FrameLayout
 *
 * @author fly
 */
public class DraggableLayout extends FrameLayout {

    private int mDownPreX = 0;
    private int mDownPreY = 0;
    private int mMovePreX = 0;
    private int mMovePreY = 0;
    private int mTranslationX, mTranslationY;
    private OnClickListener mClickListener;

    public DraggableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DraggableLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownPreX = x;
                mDownPreY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mMovePreX;
                int deltaY = y - mMovePreY;
                if (isOutOfLeftRange()) {
                    mTranslationX += 1;
                } else if (isOutOfRightRange()) {
                    mTranslationX -= 1;
                } else {
                    mTranslationX = (int) getTranslationX() + deltaX;
                }
                if (isOutOfTopRange()) {
                    mTranslationY += 1;
                } else if (isOutOfBottomRange()) {
                    mTranslationY -= 1;
                } else {
                    mTranslationY = (int) getTranslationY() + deltaY;
                }
                setTranslationX(mTranslationX);
                setTranslationY(mTranslationY);
                break;
            case MotionEvent.ACTION_UP:
                if (!isMoving(x, y) && mClickListener != null) {
                    mClickListener.onClick(this);
                }
                break;
        }
        mMovePreY = y;
        mMovePreX = x;
        return true;
    }

    /**
     * 是否在移动
     *
     * @param x
     * @param y
     * @return
     */
    private boolean isMoving(int x, int y) {
        return Math.abs(x - mDownPreX) > 20 && Math.abs(y - mDownPreY) > 20;
    }

    /**
     * 是否超出左边界
     *
     * @return
     */
    private boolean isOutOfLeftRange() {
        return getX() < 0;
    }

    /**
     * 是否超出右边界
     *
     * @return
     */
    private boolean isOutOfRightRange() {
        return getX() + getWidth() > ((ViewGroup) getParent()).getWidth();
    }

    /**
     * 是否超出上边界
     *
     * @return
     */
    private boolean isOutOfTopRange() {
        return getY() < 0;
    }

    /**
     * 是否超出下边了界
     *
     * @return
     */
    private boolean isOutOfBottomRange() {
        return getY() + getHeight() > ((ViewGroup) getParent()).getHeight();
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener clickListener) {
        this.mClickListener = clickListener;
    }

}
